/**
 * 修改密码窗口
 */
DC.front.win.CreateSiteWin = function(params, config) {
	var csWin = this;
	
	var createSite_formPanel = new DC.front.CreateSiteFormPanel();
	
	DC.front.win.CreateSiteWin.superclass.constructor.call(this, Ext.apply({
    	id : 'create_site_win',
    	title : 'Create Site',
    	width : 430,
    	height : 180,
        border : true,
        bodyBorder : true,
        layout : 'fit',
        modal : true,
        closeAction : 'close',
        items :[ createSite_formPanel ],
        buttons : [{
        	id : 'create_site_win_create_btn',
			text : 'Create',
			disabled : true,
			handler :function(){
				createSite_formPanel.create();
			} 
		}, {
			text : 'Cancle',
			handler :function(){
				csWin.close();
			} 
		}]
    }, config ||{}));
};

Ext.extend(DC.front.win.CreateSiteWin, Ext.Window, {
	
});

DC.front.CreateSiteFormPanel = function(params, config) {
	var csfp = this;
	DC.front.CreateSiteFormPanel.superclass.constructor.call(this, Ext.apply({
           id : 'createSite_formPanel',
           header : false,
           labelWidth : 80,
           labelAlign : 'right',
           buttonAlign : 'center',
           frame : true,
           defaultType : 'textfield',
           defaults : {
               width : 300,
               labelStyle : 'margin:5px auto'
           },
           items: [{
        	   id : 'createSite_formPanel_userName_field',
           	   name : 'userName',
               fieldLabel: '<font color="red">*</font>User Name',
               style:'margin:5px auto',
               allowBlank: false, // 表单控件不可以为空
               blankText:"Username can't be empty.",
               enableKeyEvents: true, // 只有将此属性设置成true，此控件的键盘事件才生效
               listeners : {
	        		keyup : function() {
	        			var created_btn = Ext.getCmp('create_site_win_create_btn');
	        			if (csfp.formFieldIsNotEmpty()) {
	        				created_btn.enable();
	        			} else {
	        				created_btn.disable();
	        			}
	        		}
	           }
           }, {
        	   id : 'createSite_formPanel_password_field',
           	   inputType: 'password',
               name: 'password',
               fieldLabel: '<font color="red">*</font>Password',
               style:'margin:5px auto',
               allowBlank: false, // 表单控件不可以为空
               blankText:"Password can't be empty.",
               enableKeyEvents: true, // 只有将此属性设置成true，此控件的键盘事件才生效
               listeners : {
	        		keyup : function() {
	        			var created_btn = Ext.getCmp('create_site_win_create_btn');
	        			if (csfp.formFieldIsNotEmpty()) {
	        				created_btn.enable();
	        			} else {
	        				created_btn.disable();
	        			}
	        		}
	           }
           }, {
        	   id : 'createSite_formPanel_url_field',
           	   name: 'url',
               fieldLabel: '<font color="red">*</font>Url',
               style:'margin:5px auto',
               allowBlank: false, // 表单控件不可以为空
               blankText:"Url can't be empty.",
               regex : /^http:\/\/.*$/,
               regexText : 'The site must be "http://" beginning.',
               enableKeyEvents: true, // 只有将此属性设置成true，此控件的键盘事件才生效
               listeners : {
	        		keyup : function() {
	        			var created_btn = Ext.getCmp('create_site_win_create_btn');
	        			if (csfp.formFieldIsNotEmpty()) {
	        				created_btn.enable();
	        			} else {
	        				created_btn.disable();
	        			}
	        		}
	           }
           }]
   }, config ||{}));
};

Ext.extend(DC.front.CreateSiteFormPanel, Ext.form.FormPanel, {
	 // 添加用户的响应事件
	create : function() {
		if (this.getForm().isValid()) {
			DC.util.CommonAction.loadMask("Saving...");
			this.getForm().submit({
		           clientValidation: true,
		           method: 'post',
		           url: DC.url.CREATE_SITE,
		           success: function(form, action){
	                	DC.util.CommonAction.unMask();
	                	var res = action.result;
	                	if (res.success) {
	                		Ext.getCmp("create_site_win").close();
	                		Ext.example.msg('Prompt', res.msg, 'prompt');
	                	} else {
	                		Ext.example.msg('Prompt', res.msg, res.type);
	                	}
		           },
		           failure: function(form, action){
		        	   DC.util.CommonAction.unMask();
		               switch (action.failureType) {
			                case Ext.form.Action.CLIENT_INVALID: // 当客户端验证失败时执行
			                     break;
			                case Ext.form.Action.CONNECT_FAILURE:
			                	 Ext.example.msg('Prompt Win', 'Network connection failure, so fail to commit.', 'error');
			                     break;
			                case Ext.form.Action.SERVER_INVALID: // 当服务器端的success属性的值返回false时执行
			                	 Ext.example.msg('Prompt Win', action.result.message, 'error');
			            }
		           }
		     });
		}
   },
   
   formFieldIsNotEmpty : function() {
	   var userName = Ext.getCmp('createSite_formPanel_userName_field').getValue().trim();
	   var password = Ext.getCmp('createSite_formPanel_password_field').getValue().trim();
	   var url = Ext.getCmp('createSite_formPanel_url_field').getValue().trim();
	   if (userName != '' && password != '' && url != '') {
		   return true;
	   }
	   return false;
   }
});